﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace HaiFang
{
    /// <summary>
    /// Summary description for ModuleInfoShowHandler
    /// </summary>
    public class ModuleInfoShowHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            if (!String.IsNullOrEmpty(context.Request.QueryString["action"]))
            {
                string action = context.Request.QueryString["action"];
                switch (action)
                {
                    case "GetModuleSubFieldInfo":
                        {
                            string Params = context.Request.QueryString["Params"];
                            ResponseGetModuleSubFieldInfo(context, Params);
                        }
                        break;
                    default:
                        WebHelper.WriteJsonError(context, "未知的操作.");
                        break;
                }
            }
            else if (context.Request.Form.Count > 0)
            {
                string action = context.Request.Form["action"];
                switch (action)
                {
                    case "InsertModuleSubTable":
                        {
                            string Params = context.Request.Form["Params"];
                            if (!string.IsNullOrEmpty(Params))
                            {
                                ResponseInsertModuleSubTable(context, Params);
                            }
                        }
                        break;
                    case "DelModuleSubTable":
                        {
                            string Params = context.Request.Form["Params"];
                            if (!string.IsNullOrEmpty(Params))
                            {
                                ResponseDelModuleSubTable(context, Params);
                            }
                        }
                        break;
                    case "SaveModuleSubTable":
                        {
                            string Params = context.Request.Form["Params"];
                            if (!string.IsNullOrEmpty(Params))
                            {
                                ResponseSaveModuleSubTable(context, Params);
                            }
                        }
                        break;
                    case "QueryModuleSubTable":
                        {
                            string Params = context.Request.Form["Params"];
                            ResponseQueryModuleSubTable(context, Params);
                        }
                        break;
                    case "QueryModuleSubTableAutoID":
                        {
                            string Params = context.Request.Form["Params"];
                            ResponseQueryModuleSubTableAutoID(context, Params);
                        }
                        break;
                    default:
                        WebHelper.WriteJsonError(context, "未知的操作");
                        break;
                }
            }
        }

        private void ResponseGetModuleSubFieldInfo(HttpContext context, string Params)
        {
            try
            {
                IList<ModuleSubFieldInfo> list = ProxyModuleSubFieldInfo.QueryModuleSubFieldInfo(WebHelper.GetCurrentUserID(), Convert.ToInt32(Params), 0);
                WebHelper.WriteJsonResult(context, list);
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
                WebHelper.WriteJsonError(context, null);
            }
        }

        private void ResponseQueryModuleSubTable(HttpContext context, string Params)
        {
            try
            {
                int RowNum = 0;
                int PageNum = 0;
                ModuleSubTableRequest r = null;
                if (!String.IsNullOrEmpty(Params))
                {
                    r = JsonHelper.JsonDeserialize<ModuleSubTableRequest>(Params);
                    if (r != null)
                    {
                        ModuleSubTableInfo list = ProxyModuleSubTable.QueryModuleSubTable(WebHelper.GetCurrentUserID(), r.ModuleSubID,
                                    r.QueryString, r.PageFirst, r.PageRow, r.PageJump, out RowNum, out PageNum);
                        WebHelper.WriteJsonResult(context, list);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            WebHelper.WriteJsonError(context, null);
        }

        private void ResponseInsertModuleSubTable(HttpContext context, string Params)
        {
            try
            {
                int ErrorMSG = -1;
                ModuleSubTableInsert r = null;
                if (!String.IsNullOrEmpty(Params))
                {
                    r = JsonHelper.JsonDeserialize<ModuleSubTableInsert>(Params);
                    if (r != null)
                    {
                        ProxyModuleSubTable.InsertModuleSubTable(WebHelper.GetCurrentUserID(), 1, 0, r.ModuleSubID, r.SqlString, out ErrorMSG);
                        WebHelper.WriteJsonResult(context, ErrorMSG);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            WebHelper.WriteJsonError(context, null);
        }

        private void ResponseDelModuleSubTable(HttpContext context, string Params)
        {
            try
            {
                int ErrorMSG = -1;
                ModuleSubTableInsert r = null;
                if (!String.IsNullOrEmpty(Params))
                {
                    r = JsonHelper.JsonDeserialize<ModuleSubTableInsert>(Params);
                    if (r != null)
                    {
                        ProxyModuleSubTable.InsertModuleSubTable(WebHelper.GetCurrentUserID(), 3,r.AutoID, r.ModuleSubID, r.SqlString, out ErrorMSG);
                        WebHelper.WriteJsonResult(context, ErrorMSG);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            WebHelper.WriteJsonError(context, null);
        }

        private void ResponseSaveModuleSubTable(HttpContext context, string Params)
        {
            try
            {
                int ErrorMSG = -1;
                ModuleSubTableInsert r = null;
                if (!String.IsNullOrEmpty(Params))
                {
                    r = JsonHelper.JsonDeserialize<ModuleSubTableInsert>(Params);
                    if (r != null)
                    {
                        ProxyModuleSubTable.InsertModuleSubTable(WebHelper.GetCurrentUserID(), 2, r.AutoID, r.ModuleSubID, r.SqlString, out ErrorMSG);
                        WebHelper.WriteJsonResult(context, ErrorMSG);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            WebHelper.WriteJsonError(context, null);
        }

        private void ResponseQueryModuleSubTableAutoID(HttpContext context, string Params)
        {
            try
            {
                ModuleSubTableSingleRequest r = null;
                if (!String.IsNullOrEmpty(Params))
                {
                    r = JsonHelper.JsonDeserialize<ModuleSubTableSingleRequest>(Params);
                    if (r != null)
                    {
                        ModuleSubTable mst = ProxyModuleSubTable.QueryModuleSubTableAutoID(WebHelper.GetCurrentUserID(), r.ModuleSubID, r.AutoID);
                        WebHelper.WriteJsonResult(context, mst);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            WebHelper.WriteJsonError(context, null);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}